package com.ybfenpan.controller.admin.member;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Daili;
import com.ybfenpan.model.Member;
import com.ybfenpan.service.DailiService;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.utils.MapUtil;
import com.ybfenpan.utils.MenuTreeNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class TuRecommendController extends BaseController {
	@Resource
    private MemberService memberService;
	@Resource
    private DailiService dailiService;
   
   

    @RequestMapping("admin/member/tuRecommend.shtml")
    public String tuRecommend(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        String loginName = MapUtil.getString(formMap, "loginName");
       
        Member self = null;
         
        if(loginName==null || loginName.length()<1){
			self=memberService.executFirst();
			if(self!=null){
				loginName=self.getLoginName();
			}
			
		}else{
			Member one=memberService.executMemberByLoginName(loginName);
			if(one==null){
				self=memberService.executFirst();
			}else{
				self=one;
			}
			
			if(self!=null){
				loginName=self.getLoginName();
			}
			
			 
		}
        Integer id = null;
        if(self!=null){
        	id = self.getId();
		}
       
        request.setAttribute("id",id);
        
        request.setAttribute("loginName",loginName);
       
        return "admin/member/tuRecommend";
       
    }
    
    @RequestMapping("admin/member/tuRecommendShow.shtml")
    public String tuRecommendShow(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        Member member = null;
        if(id>0){
        	member=memberService.getByKey(id);
        }
         
        request.setAttribute("member",member);
         
        return "admin/member/tuRecommendShow";
       
    }
   
    @RequestMapping("admin/member/ajaxTuRecommend.shtml")
    @ResponseBody
    public void ajaxTuRecommendAction(HttpServletRequest request, HttpServletResponse response) {
	    
    	Map formMap = getParameterMap(request);
	    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    
	    //需要传到页面的值
	    List<MenuTreeNode> menuList = new ArrayList();;
	    int id = MapUtil.getIntValue(formMap,"id");
	    String loginName = MapUtil.getString(formMap,"loginName");
	    
	    
	    
	    if(id<=0){
	    	
	    	
	    	
	    	Member self=null;
	    	
	    	if(loginName==null || loginName.length()<1){
				self=memberService.executFirst();
			}else{
				Member one=memberService.executMemberByLoginName(loginName);
				if(one==null){
					self=memberService.executFirst();
				}else{
					self=one;
				}
			}
	    	 
	    	if(self!=null){
	    		
	    		loginName=self.getLoginName();
	    		
				Map<String,Object> map = new HashMap();
		        map.put("accountActive","T");
		        map.put("where"," 1=1 and ( id="+self.getId()+" or recommendId = "+self.getId()+" ) order by id desc ");
		        
		        //条件查询
		        List<Member> personList=memberService.queryForList("select",map);
				
				//组装菜单节点集合
				
				for(Member person:personList){
					 
					MenuTreeNode node=new MenuTreeNode();
					node.setId(person.getId());
					
					if(person.getRecommendId()!=null){
						node.setFatherId(person.getRecommendId());
					}else{
						node.setFatherId(0);
					}
						
					
					Integer dailiId = person.getDailiId();
					Daili daili =this.dailiService.getByKey(dailiId);
					 
					String dailiTitle = daili.getTitle();  //代理商级别
					System.out.println(person.getLoginName());
					System.out.println(dailiTitle);
					System.out.println(person.getRecommendNum());
					System.out.println(person.getRecommendTotalNum());
					System.out.println(person.getActiveDate());
					node.setName("[代理商账号："+person.getLoginName()+"][代理商级别："+dailiTitle+"][直推人数:"+person.getRecommendNum()+"][团队人数:"+person.getRecommendTotalNum()+"][报单时间:"+fmt.format(person.getActiveDate())+"]");
					node.setUrl("admin/member/tuRecommendShow.shtml?id="+person.getId());
					node.setTarget("show");
					node.setChildNum(person.getRecommendNum());
					menuList.add(node);
					 
				}

			}
	    	
	    }else{
	    	 
			Map map = new HashMap();
	        map.put("accountActive","T");
	        map.put("recommendId",id);
	        map.put("where"," 1=1 order by id desc ");
	        //条件查询
	        List<Member> personList=memberService.queryForList("select",map);
			//组装菜单节点集合
			menuList=new ArrayList<MenuTreeNode>();
			for(Member person:personList){
				
				MenuTreeNode node=new MenuTreeNode();
				node.setId(person.getId());
				
				if(person.getRecommendId()!=null){
					node.setFatherId(person.getRecommendId());
				}else{
					node.setFatherId(0);
				}

				Integer dailiId = person.getDailiId();
				Daili daili =this.dailiService.getByKey(dailiId);
				 
				String dailiTitle = daili.getTitle();  //代理商级别
				
				node.setName("[代理商账号："+person.getLoginName()+"][代理商级别："+dailiTitle+"][直推人数:"+person.getRecommendNum()+"][团队人数:"+person.getRecommendTotalNum()+"][报单时间:"+fmt.format(person.getActiveDate())+"]");
				node.setUrl("admin/member/tuRecommendShow.shtml?id="+person.getId());
				node.setTarget("show");
				node.setChildNum(person.getRecommendNum());
				menuList.add(node);
				
				
			}
	    }
	    HashMap resultformMap = new HashMap();
	    resultformMap.put("menuList", menuList);
	    resultformMap.put("id", id);
	    resultformMap.put("leven", 0);
	    resultformMap.put("loginName", loginName);
	     
	    renderSuccess(response, true, resultformMap);
	    
	   
    }
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
